<template>
  <div :style="{ width: width,height:height }" style="margin-bottom: 10px;">
    <el-input
        placeholder="请粘贴"
        type="text"
        @paste="pasteImg($event)"
        clearable autocomplete="off"
        :style="{ width: width,height:height }"
    ></el-input>
  </div>
</template>

<script>
import config from "@/config/upload";
export default {
  components: {},
  emits: ['success', 'closed'],
  props: {
    apiObj: { type: Object, default: () => {} },
    width: { type: String, default:"100%"},
    height: { type: String, default:"30px"},
  },
  data() {
    return { }
  },
  mounted() {

  },
  methods: {
    pasteImg(event){
      console.log(event,'1111')
      //粘贴上传图片
      let txt = event.clipboardData.getData('Text')
      if(typeof(txt) == 'string' && txt !== '') {
        //检测到是文字，过滤
        return ;
      }
      let file = null
      const items = (event.clipboardData || window.clipboardData).items
      if(items.length) {
        for (let i = 0; i < items.length; i++) {
          if (items[i].type.indexOf('image') !== -1) {
            file = items[i].getAsFile()
            this.handleChange(file)
            if (!this.canUpload) {
              this.canUpload = !this.canUpload;
            }
            break
          }
        }
      }
    },
    error(err){
      this.$notify.error({
        title: '上传文件未成功',
        message: err
      })
    },
    // 图片上传
    handleChange(param) {
      var apiObj = config.apiObj;
      if(this.apiObj){
        apiObj = this.apiObj;
      }
      let data = new FormData()
      // 需要token
      // formData.append('token',  this.dataObj.token)
      data.append('file', param)
      apiObj.post(data, {}).then(res => {
        console.log('res',res)
        var response = config.parseData(res);
        if(response.code == config.successCode){
          this.$emit('success', response)
        }else{
          this.error(response.msg || "未知错误")
        }
      }).catch(err => {
        this.error(err)
      })
    },
  }
}
</script>

<style lang="scss">
</style>
